package service_manager

import (
	"fmt"
	"gopkg/app/interfaces"
	"gopkg/app/internal/module"
)

func NewServiceManager() interfaces.ServiceManager {
	sm := &serviceManager{
		serviceMap: make(map[string]interfaces.Service),
	}
	return sm
}

type serviceManager struct {
	module.ModuleLifeManager
	serviceMap map[string]interfaces.Service
}

func (svm *serviceManager) RegisterService(s interfaces.Service) {
	_, exist := svm.serviceMap[s.Name()]
	if exist {
		panic(fmt.Sprintf("service [%v] duplicate", s.Name()))
	}
	svm.serviceMap[s.Name()] = s
	svm.RegisterModule(s)
}

func (svm *serviceManager) GetService(name string) interfaces.Service {
	return svm.serviceMap[name]
}
